package com.jpz.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jpz.bean.Course;
import com.jpz.bean.Permission;
import com.jpz.bean.Role;
import com.jpz.bean.RolePermission;
import com.jpz.service.PermissionService;
import com.jpz.service.RolePermissionService;
import com.jpz.service.RoleService;
import com.jpz.utils.pageshow;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jpz
 * @since 2018-11-14
 */
@Controller
@RequestMapping("/role")
public class RoleController {
	@Autowired
	RoleService rs;
	@Autowired
	PermissionService ps;
	@Autowired
	RolePermissionService rps;
	@RequestMapping("/show")
	public String show(Model model,Integer pageindex) {
		Page<Role> page = new Page<>(pageindex, 4);
		Page<Role> page2 = rs.selectPage(page, new EntityWrapper<Role>().eq("available", "1"));
		List<Role> lis = page2.getRecords();
		long total = page2.getTotal();
		int ii= new Long(total).intValue();  
		pageshow<Role> pageshow = new pageshow<>(lis, ii, null, page2.getCurrent());
		model.addAttribute("pagebean", pageshow);
		return "/rolelist.jsp";
	}
	@ResponseBody
	@RequestMapping("/toUpdate")
	public Map<String, Object> toup(Integer rid){
		int id = rid;

		Role role = rs.selectById(id);
		List<Permission> menus = ps.selectList(new EntityWrapper<Permission>().eq("type", "menu").eq("available", 1));
		for (Permission menu : menus) {
			Integer menuId = menu.getId();  //菜单的ID 判断是否在我的中间表中
			int count = rps.selectCount(new EntityWrapper<RolePermission>().eq("sys_role_id", id).eq("sys_permission_id", menuId));
			if(count>0){
				//中间有
				menu.setFlag(true);
			}
		}
		List<Permission> permissions = ps.selectList(new EntityWrapper<Permission>().eq("type", "permission").eq("available", 1));
		for (Permission permission : permissions) {
			Integer permissionId = permission.getId();  //菜单的ID 判断是否在我的中间表中
			int count = rps.selectCount(new EntityWrapper<RolePermission>().eq("sys_role_id", id).eq("sys_permission_id", permissionId));
			if(count>0){
				//中间有
				permission.setFlag(true);
			}
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("role", role);
		map.put("menus", menus);
		map.put("permissions", permissions);
		Set<Entry<String,Object>> set = map.entrySet();
		for (Entry<String, Object> entry : set) {
			System.out.println(entry);
		}
		return map;
	}
	@ResponseBody
	@RequestMapping("/update")
	public boolean update(Role r,Integer[] menus_ids,Integer[] permission_ids){
		r.setAvailable("1");
		return rs.updateRole(r, menus_ids, permission_ids);
	}
	@ResponseBody
	@RequestMapping("/toadd")
	public Map<String, Object> getMuenusAndPermions() {
		List<Permission> menus = ps.selectList(new EntityWrapper<Permission>().eq("type", "menu").eq("available", 1));
		List<Permission> permissions = ps.selectList(new EntityWrapper<Permission>().eq("type", "permission").eq("available", 1));
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("menus", menus);
		map.put("permissions", permissions);
		return map;
	}
	@ResponseBody
	@RequestMapping("/add")
	public boolean add(Role r,Integer[] menus_ids,Integer[] permission_ids){
		r.setAvailable("1");
		return rs.addRole(r, menus_ids, permission_ids);
	}

}

